package com.sky.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * user根据套餐id查询包含的菜品列表
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {

        //"select sd.name, sd.copies, d.image, d.description " +
        //            "from setmeal_dish sd left join dish d on sd.dish_id = d.id " +
        //            "where sd.setmeal_id = #{setmealId}"

        List<DishItemVO> dishItemVOS = setmealDishMapper.selectJoinList(DishItemVO.class, new MPJLambdaWrapper<DishItemVO>()
                .selectAll(DishItemVO.class)
                .leftJoin(Dish.class, Dish::getId, SetmealDish::getDishId)
                .eq(SetmealDish::getId, id)
        );

        return dishItemVOS;
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表插入一条数据
        save(setmeal);

        //setmeal的id自增长 获取insert语句生成的主键值  去数据库获取该数据
        Setmeal dishMysql = query().eq("name", setmeal.getName()).one();
        //数据库setmeal的id值
        Long setmealId = dishMysql.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (setmealDishes != null && setmealDishes.size()>0){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
                //向套餐菜品表插入n条数据
                setmealDishService.save(setmealDish);
            }
        }
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Override
    public void updateWithSetmealDish(SetmealDTO setmealDTO) {
        //根据套餐查询套餐菜品
        List<SetmealDish> setmealDishList = setmealDishService.lambdaQuery().eq(SetmealDish::getSetmealId, setmealDTO.getId()).list();
        for (SetmealDish setmealDish : setmealDishList) {
            //删除套餐菜品
            setmealDishService.removeById(setmealDish);
        }

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //更新套餐
        updateById(setmeal);

        //更新套餐菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if(setmealDishes != null && setmealDishes.size() > 0){
            for (SetmealDish setmealDish : setmealDishes) {
                //这里不需要，setmeal_Id没有变
//                setmealDish.setSetmealId(setmealDish.getId());
                setmealDishService.updateById(setmealDish);
            }
        }

    }

    /**
     * 套餐起售停售
     * @param status
     * @param id
     * @return
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal build = Setmeal.builder().id(id).status(status).build();
        updateById(build);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        String name = setmealPageQueryDTO.getName();
        Integer categoryId = setmealPageQueryDTO.getCategoryId();
        Integer status = setmealPageQueryDTO.getStatus();
        //分页条件
        Page<Setmeal> pageCondition = Page.of(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
//        //排序条件
//        pageCondition.addOrder(new OrderItem("price",true))
//                .addOrder(new OrderItem("create_time",false));
        //分页查询 select s.* , c.name as categoryName from Setmeal s left outer join categoryId c on s.categoryId =c.id where name = ? and categoryId = ? and status =?
        IPage<SetmealVO> page = setmealMapper.selectJoinPage(new Page<>(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize()), SetmealVO.class,
                new MPJLambdaWrapper<SetmealVO>()
                        .selectAll(Setmeal.class) // 选择Dish的所有字段
                        .selectAs(Category::getName, SetmealVO::getCategoryName) // 选择Category的name字段 as categoryName
                        .leftJoin(Category.class, Category::getId,Setmeal::getCategoryId) // 左连接Category表
                        .eq(name!=null, Setmeal::getName, name)
                        .eq(categoryId!=null, Setmeal::getCategoryId,categoryId)
                        .eq(status!=null, Setmeal::getStatus,status)
                        .orderByDesc(Setmeal::getCreateTime)
        );

        return new PageResult(page.getTotal(), page.getRecords());
    }


}
